﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using You2V.LocalDBLayer;

namespace You2V.Data.Shared
{
	[Serializable]
	public class Student
	{
		public Student()
		{
        }

        public Guid Student2012ID { get; set; }     //来自Student2012的GUID
        public int tc_id { get; set; }
        public string StudentNumber { get; set; }
        public string FullName { get; set; }
        public string Gender { get; set; }
        public string SchoolShortName { get; set; }
        public string ClassName { get; set; }

        public StudentInfo2012 tc_info 
        {
            get
            {
                if (m_tc_info == null)
                {
                    if (Student2012ID != Guid.Empty)
                    {
                        StudentDBL StudentDBL = new StudentDBL();
                        m_tc_info = StudentDBL.GetStudent2012ByID(Student2012ID);
                    }
                }
                return m_tc_info;
            }
            set
            { m_tc_info = value; }
        }
        private StudentInfo2012 m_tc_info;

		public StudentEducationInfo tc_currentEduInfo
		{
			get
			{
                if (tc_info != null)
                {
                    StudentDBL StudentDBL = new StudentDBL();
                    return StudentDBL.GetAvaliableStudentEduInfo(tc_info.ID);
                }
                else
                    return null;
			}
		}//当前学生联系方式

		public Sponsor tc_sponsor
		{
			get
			{
				if (m_sponsor == null)
					GetSponser();
				return m_sponsor;
			}
		}
		private Sponsor m_sponsor;

		public StudentPeriodCollection tc_period
		{
			get
			{
				if (m_tc_period == null)
					GetValidStudentPeriod();
				return m_tc_period;
			}
		}
		private StudentPeriodCollection m_tc_period;

		private void GetSponser()
		{
			m_sponsor = SponsorDBL.Instance.GetStudentLinkedSponsorFromDB(m_tc_info.ID);
			if (m_sponsor == null)
			{
				Sponsor _sponsor = SponsorDBL.Instance.GetSponsorByStudentID(tc_id);
				if(_sponsor != null)
					m_sponsor = SponsorDBL.Instance.CreateStudentSponsorLink(m_tc_info.ID, _sponsor.ID);
			}
		}
		private void GetValidStudentPeriod()
		{
            StudentDBL StudentDBL = new StudentDBL();
			m_tc_period = StudentDBL.GetStudentPeriod(m_tc_info.ID);
		}
		public bool UpdateEduInfo(string school, string className, string amount, string volunteer, string contact, string teacher)
		{
            StudentDBL StudentDBL = new StudentDBL();
			StudentEducationInfo _currentEduInfo = StudentDBL.GetAvaliableStudentEduInfo(tc_info.ID);
			if (school == _currentEduInfo.schoolName &&
				className == _currentEduInfo.className &&
				amount == _currentEduInfo.amount &&
				volunteer == _currentEduInfo.volunteer &&
				contact == _currentEduInfo.contact &&
				teacher == _currentEduInfo.teacher)
			{ return false; }

			StudentDBL.InsertNewStudentEduInfo(tc_info.ID, school, className, amount, volunteer, teacher, contact);
			return true;
		}
		public void AddNewPeriodAction(string Period, string Action, int Amount, string Description, DateTime ActionTime)
		{
			StudentPeriodAction _adding = new StudentPeriodAction();
			_adding.Period = Period;
			_adding.action = Action;
			_adding.amount = Amount;
			_adding.description = Description;
			_adding.edu_info = tc_currentEduInfo;
            _adding.actionDate = ActionTime;

            StudentDBL StudentDBL = new StudentDBL();
			StudentDBL.InserNewStudentPeriodAction(_adding);
			this.GetValidStudentPeriod();
		}
		public void DisablePeriodAction(Guid ID, string delDesc)
		{
            StudentDBL StudentDBL = new StudentDBL();
			StudentDBL.DisableStudentPeriodAction(ID, delDesc);
			this.GetValidStudentPeriod();
		}
	}


	public class StudentEducationInfo
	{
		public Guid tceduinfo_id { get; set; } //本地生成GUID
		public Guid tc2012_id { get; set; }	 //Student2012ID, 新表
		public string schoolName { get; set; }	 //学校名称
		public string className { get; set; }	 //班级名称
		public string amount { get; set; }		 //建议资助标准
		public string volunteer { get; set; }	 //志愿者
		public string teacher { get; set; }		 //老师
		public string contact { get; set; }		 //紧急联系人
		public bool enabled { get; set; }		//是否为当前联系方式
		public DateTime CreatedDate { get; set; }  //创建时间
		public string CreatedBy { get; set; }      //创建人
	}

	public class StudentPeriodCollection
	{
		public StudentPeriodCollection()
		{
			_items = new Dictionary<string, StudentPeriod>();
		}

		public Dictionary<string, StudentPeriod> Items
		{ get { return _items; } }
		private Dictionary<string, StudentPeriod> _items;

		public bool Contains(string Period)
		{
			return _items.Keys.Contains(Period);
		}
		public StudentPeriod Period(string PeriodName)
		{
			if (Contains(PeriodName))
				return _items[PeriodName];

			StudentPeriod _newperiod = new StudentPeriod(PeriodName);
			_items[PeriodName] = _newperiod;
			return _newperiod;
		}

		public Dictionary<string, StudentPeriod>.KeyCollection ValidPeriod
		{
			get
			{ return _items.Keys; }
		}
		public DataTable GetPeriodDataGrid(string PeriodName)
		{
			if (!Contains(PeriodName))
				throw new ObjectNotFoundException(string.Format("当前学生中无法找到数据： {0}", PeriodName));
			DataTable dt = GetPeriodTable();
			DataRow _r = dt.NewRow();


			foreach (StudentPeriodAction _action in _items[PeriodName].periodActions)
			{
				string _temp, _temp2; 
				if(dt.Columns.Contains(_action.action) && _action.enabled)
				{
					_temp = _action.actionDate.ToShortDateString();
					if (StudentActionEnum.items[_action.action].ShowAccount)
					{
						_temp += string.Format(" {0}元", _action.amount);
					}
					//_temp += " :" + _action.MoidifiedBy;

					if(_r[_action.action].ToString() != string.Empty)
						_r[_action.action] += "\n";
					_r[_action.action] += _temp;
				}

				_temp2 = "\n" + _action.actionDate.ToString() + " " + _action.action;
				
				if (StudentActionEnum.items[_action.action].ShowAccount)
				{
					_temp2 += string.Format(" 金额：{0}元", _action.amount);
				}
				if (!_action.enabled)
				{
 					_temp2 += string.Format("(由{0}于{1}撤销)",_action.MoidifiedBy, _action.ModifiedTime);
					_temp2 += "\n    撤销原因：" + _action.delDesc;
				}

				_temp2 += string.Format("\n    学生信息：{0},{1} 老师:{2} 联系人:{3}\n", _action.edu_info.schoolName, _action.edu_info.className, _action.edu_info.teacher, _action.edu_info.contact);
				_temp2 += string.Format("    备注：{0}\n", _action.description);
				_temp2 += string.Format("    资料员：{0}\n", _action.MoidifiedBy);
				_r["periodActionSummary"] += _temp2;

			}
			dt.Rows.Add(_r);
			return dt;
		}
		public static DataTable GetPeriodTable()
		{
			DataTable dt = new DataTable("PeriodAction");
			foreach (StudentActionEnum p_action in StudentActionEnum.items.Values)
			{
				if (p_action.ShowedAsPrimary)
					dt.Columns.Add(p_action.action, typeof(string));
			}
			dt.Columns.Add("periodActionSummary", typeof(string));
			return dt;
		}
	}

    public class StudentPeriod
    {
		public StudentPeriod(string key)
		{
			Period = key;
			periodActions = new List<StudentPeriodAction>();
		}

		public string Period { get; set; }
		public List<StudentPeriodAction> periodActions { get; set; }

		public bool Equals(string m_PeriodName)
		{
			return m_PeriodName == Period;
		}
		public void AddStuPeriodAction(StudentPeriodAction action)
		{
			if (action != null)
				periodActions.Add(action);
		}
    }

    public class StudentPeriodAction
    {
        public Guid ID { get; set; }
		public StudentEducationInfo edu_info { get; set; }
		public string Period { get; set; }
        public string action { get; set; }
		public int amount { get; set; }
		public DateTime actionDate { get; set; } //7.25操作日期改在这里，孔令钧
		public string description { get; set; }
		public string delDesc { get; set; }
		public bool enabled { get; set; }
        public DateTime CreateTime { get; set; }    //成刚7.19 - 用作操作日期，允许补录，比如7/17录入7/10进行的操作，此时CreateTime是7/10, ModifiedTime是7/17
		public string CreatedBy { get; set; }      //创建人
		public DateTime ModifiedTime { get; set; }
		public string MoidifiedBy { get; set; }
		public string Agenda
		{
			get
			{
				string actiontemp = action + " ";
				if(StudentActionEnum.items[action].ShowAccount)
					actiontemp += amount;
				return string.Format("{0} {1} 操作员:{2}", actionDate.ToShortDateString(), actiontemp, CreatedBy);
			}
		}
    }

	public class StudentFamilyMember
	{
		public string RelationShip;
		public string Name;
		public string Age;
		public string Job;
		public string Health;
		public string JobStatus;
 		//关系 姓名 年龄 职业/所在学校 健康情况 外出打工情况 
	}

	
}
